<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Statics extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->checkAdmin($this->session->userdata('id_user'));
        $this->checkPermission($this->session->userdata('level'), array(1));
        $this->data['controller'] = 'statics';
        $this->load->model('Mstatics');
        $this->load->model('Mindex');
    }
    
    public function index()
    {
        $this->data['id_active'] = $this->getParamUri(2,3);
        $this->load->library('pagination');
        $config['per_page'] = 10;
        $this->data['page'] = $this->getParamUri(2,3);
        $data = $this->Mstatics->getCampaignDB($config['per_page'],(($this->getParamUri(2,3) - 1) * $config['per_page'] > 0 ? ($this->getParamUri(2,3) - 1) * $config['per_page'] : 0),$this->session->userdata('id_user'));
        $config['base_url'] = base_url($this->uri->segment(1).'/');//$this->uri->segment(2).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 2;
        $config['uri_segment_page'] = $this->getParamUri(2,3);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
 
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data['data'];

        //$this->data['static'] = $this->Mindex->getStatic($data['idCampaign']);
        
        $this->data['action'] = 'index';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function detail()
    {

        $this->data['id'] = $this->getParamInt('id');
        $time_select = $this->getParamString('time_select');
        
        $id_campaign = intval($this->Mstatics->getIDCampaign($this->data['id']));
        $this->data['nameShop'] = $this->Mstatics->getNameShop($this->data['id']);
        $data_time = $this->Mstatics->getTimeCampaign($id_campaign,$this->session->userdata('id_user'));
        $this->data['title'] = $data_time->title;
        if(!in_array($time_select, array(null, '', '0')))
        {
            $this->data['time_select'] = $time_select;
            $this->data['time'] = $this->getParamString('time');
        }else{
            $this->data['time_select'] = $data_time->time_select;
            $this->data['time'] = $data_time->time;
        }
        
        $this->data['type'] = $this->getParamInt('type');
        $this->data['chart_ab'] = $this->Mstatics->getChartAb($id_campaign, $this->data['time'], $this->data['type'], $this->session->userdata('id_user'));
        
        $this->data['id_active'] = $this->getParamUri(2,3);
        $this->load->library('pagination');
        $config['per_page'] = 25;
        $this->data['page'] = $this->getParamUri(2,3);
        $this->data['per_page'] = $config['per_page'];
        if(!in_array($this->data['id'], array(null, '', '0')) || !in_array($this->data['time_select'], array(null, '', '0')))
        {
            $config['paramhack'] = '?id='.$this->data['id'].'&type='.$this->data['type'].'&time='.$this->data['time'].'&time_select='.$this->data['time_select'].'#page';
        }
        
        $offer = (($this->getParamUri(2,3) - 1) * $config['per_page'] > 0 ? ($this->getParamUri(2,3) - 1) * $config['per_page'] : 0);
        $id_user = $this->session->userdata('id_user');
        $data = $this->Mstatics->getLogAccess($config['per_page'],$offer,$id_user,$id_campaign,$this->data['time']);
        
        
        $config['base_url'] = base_url('page_ajax_detail/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 2;
        $config['uri_segment_page'] = $this->getParamUri(2,3);
        $config['suffix'] = '.html';
        $config['ellipsize'] = TRUE;
        $config['stt_reduced']  = 10;
        $config['reduced']  = 10;
        $this->pagination->initialize($config);
 
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data['data'];
        
        
        $_data = $this->Mstatics->getCount($this->session->userdata('id_user'),$this->data['time'],$id_campaign);
        //echo "<pre>";die(print_r($_data));
        $this->data['browser'] = $_data->browser;
        $this->data['browser_ver'] = $_data->browser_ver;
        $this->data['device_name'] = $_data->device_name;
        $this->data['os_group'] = $_data->os_group;
        
        
        $this->data['show_time'] = $this->Mstatics->getCountTime($this->session->userdata('id_user'),$this->data['time'],$id_campaign, 20, 0);
        
        
        $this->data['getmail'] = $this->Mstatics->getmail($this->data['id'], $id_campaign, $this->data['time']);
        $this->data['getfacebook'] = $this->Mstatics->getfacebook($this->data['id'], $id_campaign, $this->data['time']);
		$this->data['getmobile'] = $this->Mstatics->getmobile($this->data['id'], $id_campaign, $this->data['time']);
        
        $this->data['device'] = $_data->device;
        $this->data['os'] = $_data->os;
        
        
        //$this->data['data'] = $this->Mcontact->getdetail($id);
        
        $this->data['selectType'] = $this->selectType($this->getParamInt('type'));
        $this->data['action'] = 'detail';
        $this->load->view('temp/layout', $this->data);
    }
    
    
    
    public function gettimeaccess()
    {
        $this->data['id'] = $this->getParamInt('id');
        $time_select = $this->getParamString('time_select');
        $id_campaign = intval($this->Mstatics->getIDCampaign($this->data['id']));        
        $data_time = $this->Mstatics->getTimeCampaign($id_campaign,$this->session->userdata('id_user'));
        $this->data['title'] = $data_time->title;
        if(!in_array($time_select, array(null, '', '0')))
        {
            $this->data['time_select'] = $time_select;
            $this->data['time'] = $this->getParamString('time');
        }else{
            $this->data['time_select'] = $data_time->time_select;
            $this->data['time'] = $data_time->time;
        }
        
        $this->data['show_time'] = $this->Mstatics->getCountTime($this->session->userdata('id_user'),$this->data['time'],$id_campaign, 20, $this->getParamInt('dem'));
        if(in_array($this->data['show_time'], array(null, '', '0')))
        {
            die('201');
        }
        //echo $this->getParamInt('dem');
        //die(print_r($this->data['show_time']));
        $this->load->view('temp/statics/gettimeaccess', $this->data);
    }
    
    public function page_ajax_detail()
    {
        
        $this->data['id'] = $this->getParamInt('id');
        $time_select = $this->getParamString('time_select');
        if(!in_array($time_select, array(null, '', '0')))
        {
            $this->data['time_select'] = $time_select;
            $this->data['time'] = $this->getParamString('time');
        }else{
            $data_time = $this->Mstatics->getTimeCampaign($this->data['id'],$this->session->userdata('admin_id'));
            $this->data['time_select'] = $data_time->time_select;
            $this->data['time'] = $data_time->time;
        }
        $this->data['type'] = $this->getParamInt('type');
        
        $this->data['id_active'] = $this->getParamUri(2,3);
        $this->load->library('pagination');
        $config['per_page'] = 25;
        $this->data['per_page'] = $config['per_page'];
        $this->data['page'] = $this->getParamUri(2,3);
    
        if(!in_array($this->data['id'], array(null, '', '0')) || !in_array($this->data['time_select'], array(null, '', '0')))
        {
            $config['paramhack'] = '?id='.$this->data['id'].'&type='.$this->data['type'].'&time='.$this->data['time'].'&time_select='.$this->data['time_select'].'#page';
        }
        
        $offer = (($this->getParamUri(2,3) - 1) * $config['per_page'] > 0 ? ($this->getParamUri(2,3) - 1) * $config['per_page'] : 0);
        $id_user = $this->session->userdata('admin_id');
        $data = $this->Mstatics->getLogAccess($config['per_page'],$offer,$id_user,$this->data['id'],$this->data['time']);
        
        
        $config['base_url'] = base_url($this->uri->segment(1).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 2;
        $config['uri_segment_page'] = $this->getParamUri(2,3);
        
        $config['full_tag_open'] = '<p id="pagination">';
        $config['full_tag_close'] = '</p>';
        
        $config['suffix'] = '.html';
        $config['ellipsize'] = TRUE;
        $config['stt_reduced']  = 10;
        $config['reduced']  = 10;
        $this->pagination->initialize($config);
 
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data['data'];
        
        $this->load->view('temp/statics/page_ajax_detail', $this->data);

    }
    
    public function device()
    {
        $this->data['id'] = $this->getParamInt('id');
        $data_time = $this->Mstatics->getTimeCampaign($this->data['id'],$this->session->userdata('admin_id'));
        $this->data['title'] = $data_time->title;
        $time_select = $this->getParamString('time_select');
        if(!in_array($time_select, array(null, '', '0')))
        {
            $this->data['time_select'] = $time_select;
            $this->data['time'] = $this->getParamString('time');
        }else{
            $this->data['time_select'] = $data_time->time_select;
            $this->data['time'] = $data_time->time;
        }
        $this->data['type'] = $this->getParamInt('type');

        $this->data['selectType'] = $this->selectType($this->getParamInt('type'));
        
        $_data = $this->Mstatics->getDevice($this->session->userdata('admin_id'),$this->data['time'],$this->data['id'],$this->data['type']);
        //echo "<pre>";die(print_r($_data));
        $this->data['statics_device_colum'] = $_data->colum;
        $this->data['statics_device_table'] = $_data->table;
        
        $this->data['action'] = 'device';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function browser()
    {
        $this->data['id'] = $this->getParamInt('id');
        $data_time = $this->Mstatics->getTimeCampaign($this->data['id'],$this->session->userdata('admin_id'));
        $this->data['title'] = $data_time->title;
        $time_select = $this->getParamString('time_select');
        if(!in_array($time_select, array(null, '', '0')))
        {
            $this->data['time_select'] = $time_select;
            $this->data['time'] = $this->getParamString('time');
        }else{
            $this->data['time_select'] = $data_time->time_select;
            $this->data['time'] = $data_time->time;
        }
        $this->data['type'] = $this->getParamInt('type');

        $this->data['selectType'] = $this->selectType($this->getParamInt('type'));
        
        $_data = $this->Mstatics->getBrowser($this->session->userdata('admin_id'),$this->data['time'],$this->data['id'],$this->data['type']);

        $this->data['statics_browser_table'] = $_data->table;
        $this->data['statics_browser_chart'] = $_data->chart;
        
        $this->data['action'] = 'browser';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function os()
    {
        $this->data['id'] = $this->getParamInt('id');
        $data_time = $this->Mstatics->getTimeCampaign($this->data['id'],$this->session->userdata('admin_id'));
        $this->data['title'] = $data_time->title;
        $time_select = $this->getParamString('time_select');
        if(!in_array($time_select, array(null, '', '0')))
        {
            $this->data['time_select'] = $time_select;
            $this->data['time'] = $this->getParamString('time');
        }else{
            $this->data['time_select'] = $data_time->time_select;
            $this->data['time'] = $data_time->time;
        }
        
        $this->data['type'] = $this->getParamInt('type');

        $this->data['selectType'] = $this->selectType($this->getParamInt('type'));
        
        $_data = $this->Mstatics->getOsStatic($this->session->userdata('admin_id'),$this->data['time'],$this->data['id'],$this->data['type']);
        
        $this->data['statics_os_table'] = $_data->table;
        $this->data['statics_os_chart'] = $_data->chart;
        
        $this->data['action'] = 'os';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function place()
    {
        $this->data['id'] = $this->getParamInt('id');
        $data_time = $this->Mstatics->getTimeCampaign($this->data['id'],$this->session->userdata('admin_id'));
        $this->data['title'] = $data_time->title;
        $time_select = $this->getParamString('time_select');
        if(!in_array($time_select, array(null, '', '0')))
        {
            $this->data['time_select'] = $time_select;
            $this->data['time'] = $this->getParamString('time');
        }else{
            $this->data['time_select'] = $data_time->time_select;
            $this->data['time'] = $data_time->time;
        }
        
        $this->data['type'] = $this->getParamInt('type');

        $this->data['selectType'] = $this->selectType($this->getParamInt('type'));
        
        $_data = $this->Mstatics->getPlaceStatic($this->session->userdata('admin_id'),$this->data['time'],$this->data['id'],$this->data['type']);
        //echo "<pre>";die(print_r($_data));
        $this->data['statics_place_table'] = $_data->table;
        $this->data['statics_place_chart'] = $_data->chart;
        
        $this->data['action'] = 'place';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function selectType($id)
    {
        $arr = (object)array('Tất cả','Trình duyệt','Thiết bị','Hệ điều hành');
        $html  = '';
        foreach($arr AS $el=>$le)
        {
            if($id == $el)
            {
                $selected = 'selected="selected"';
            }else{
                $selected = '';
            }
            $html .= '<option value="'.$el.'"'.$selected.'>'.$le.'</option>';
        }
        return $html;
    }
    
    
    /**
     * 
     * 
     *  @void báo cáo
     * 
     * 
     */
    public function exportreportplace()
    {
        $this->data['id'] = $this->getParamInt('id');
        $this->data['time'] = $this->getParamString('time');
        $this->data['time_select'] = $this->getParamString('time_select');
        $this->load->view('temp/statics/exportreportplaceajax', $this->data);
    }
    
    public function exportreportplacerun()
    {
        $this->data['id'] = $this->getParamInt('id');
        $time_select = $this->getParamString('time_select');
        $id_campaign = intval($this->Mstatics->getIDCampaign($this->data['id']));
        $this->data['nameShop'] = $this->Mstatics->getNameShop($this->data['id']);//die($this->data['nameShop']);
        $data_time = $this->Mstatics->getTimeCampaign($id_campaign,$this->session->userdata('id_user'));
        $this->data['title'] = $data_time->title;
        if(!in_array($time_select, array(null, '', '0')))
        {
            $this->data['time_select'] = $time_select;
            $this->data['time'] = $this->getParamString('time');
        }else{
            $this->data['time_select'] = $data_time->time_select;
            $this->data['time'] = $data_time->time;
        }
        
        $this->data['data'] = $this->Mstatics->getExport($this->session->userdata('id_user'),$this->data['time'],$id_campaign);
        //echo "<pre>";
        //die(print_r($this->data['data']));
        $this->load->view('temp/statics/exportreportplace', $this->data);
    }
    
    
    // Email
    public function exportreportemail()
    {
        $this->data['id'] = $this->getParamInt('id');
        $this->data['time'] = $this->getParamString('time');
        $this->data['time_select'] = $this->getParamString('time_select');
        $this->load->view('temp/statics/exportreportemailajax', $this->data);
    }
    
    public function exportreportemailrun()
    {
        $this->data['id'] = $this->getParamInt('id');
        $time_select = $this->getParamString('time_select');
        $id_campaign = intval($this->Mstatics->getIDCampaign($this->data['id']));
        $this->data['nameShop'] = $this->Mstatics->getNameShop($this->data['id']);//die($this->data['nameShop']);
        $data_time = $this->Mstatics->getTimeCampaign($id_campaign,$this->session->userdata('id_user'));
        $this->data['title'] = $data_time->title;
        if(!in_array($time_select, array(null, '', '0')))
        {
            $this->data['time_select'] = $time_select;
            $this->data['time'] = $this->getParamString('time');
        }else{
            $this->data['time_select'] = $data_time->time_select;
            $this->data['time'] = $data_time->time;
        }
        
        $this->data['data'] = $this->Mstatics->getExportEmail($this->session->userdata('id_user'),$this->data['time'],$id_campaign, $this->data['id']);
        //echo "<pre>";
        //die(print_r($this->data['data']));
        $this->load->view('temp/statics/exportreportemail', $this->data);
    }
    
    // Email
    public function exportreportfacebook()
    {
        $this->data['id'] = $this->getParamInt('id');
        $this->data['time'] = $this->getParamString('time');
        $this->data['time_select'] = $this->getParamString('time_select');
        $this->load->view('temp/statics/exportreportfacebookajax', $this->data);
    }
    
    public function exportreportfacebookrun()
    {
        $this->data['id'] = $this->getParamInt('id');
        $time_select = $this->getParamString('time_select');
        $id_campaign = intval($this->Mstatics->getIDCampaign($this->data['id']));
        $this->data['nameShop'] = $this->Mstatics->getNameShop($this->data['id']);//die($this->data['nameShop']);
        $data_time = $this->Mstatics->getTimeCampaign($id_campaign,$this->session->userdata('id_user'));
        $this->data['title'] = $data_time->title;
        if(!in_array($time_select, array(null, '', '0')))
        {
            $this->data['time_select'] = $time_select;
            $this->data['time'] = $this->getParamString('time');
        }else{
            $this->data['time_select'] = $data_time->time_select;
            $this->data['time'] = $data_time->time;
        }
        
        $this->data['data'] = $this->Mstatics->getExportFacebook($this->session->userdata('id_user'),$this->data['time'],$id_campaign, $this->data['id']);
        //echo "<pre>";
        //die(print_r($this->data['data']));
        $this->load->view('temp/statics/exportreportfacebook', $this->data);
    }
	
	// Email
    public function exportreportmobile()
    {
        $this->data['id'] = $this->getParamInt('id');
        $this->data['time'] = $this->getParamString('time');
        $this->data['time_select'] = $this->getParamString('time_select');
        $this->load->view('temp/statics/exportreportmobileajax', $this->data);
    }
    
    public function exportreportmobilerun()
    {
        $this->data['id'] = $this->getParamInt('id');
        $time_select = $this->getParamString('time_select');
        $id_campaign = intval($this->Mstatics->getIDCampaign($this->data['id']));
        $this->data['nameShop'] = $this->Mstatics->getNameShop($this->data['id']);//die($this->data['nameShop']);
        $data_time = $this->Mstatics->getTimeCampaign($id_campaign,$this->session->userdata('id_user'));
        $this->data['title'] = $data_time->title;
        if(!in_array($time_select, array(null, '', '0')))
        {
            $this->data['time_select'] = $time_select;
            $this->data['time'] = $this->getParamString('time');
        }else{
            $this->data['time_select'] = $data_time->time_select;
            $this->data['time'] = $data_time->time;
        }
        
        $this->data['data'] = $this->Mstatics->getExportMobile($this->session->userdata('id_user'),$this->data['time'],$id_campaign, $this->data['id']);
        //echo "<pre>";
        //die(print_r($this->data['data']));
        $this->load->view('temp/statics/exportreportmobile', $this->data);
    }
    
	public function campaignuser()
    {
        $this->data['id_active'] = $this->getParamUri(2,3);
        $this->load->library('pagination');
        $config['per_page'] = 10;
        $this->data['page'] = $this->getParamUri(2,3);
        $data = $this->Mstatics->getCampaignUser($config['per_page'],(($this->getParamUri(2,3) - 1) * $config['per_page'] > 0 ? ($this->getParamUri(2,3) - 1) * $config['per_page'] : 0),$this->session->userdata('admin_id'));
        $config['base_url'] = base_url($this->uri->segment(1).'/');//$this->uri->segment(2).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 2;
        $config['uri_segment_page'] = $this->getParamUri(2,3);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
 
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data;
        
        $this->data['action'] = 'campaignuser';
        $this->load->view('temp/layout', $this->data);
    }
    
	public function addcampaignuser(){
		$this->load->library('form_validation');
        $this->form_validation->set_rules('code_campaign', 'Mã chiến dịch', 'required');
		$this->form_validation->set_rules('title', 'Tên chiến dịch', 'required');
		$this->form_validation->set_rules('time_start', 'Thời gian bắt đầu', 'required');
        $this->form_validation->set_rules('time_end', 'Thời gian kết thúc', 'required');
        $this->data['getListShop'] = $this->Mstatics->getListShop('');
        if($this->form_validation->run() === TRUE)
        {
            $title = $this->getParamString('title');	
			$categories_id = $this->getParamInt('categories_id');
			$summary = $this->getParamString('summary');
			$content = $this->getParamString('content');
			$author_id = $this->getParamInt('author_id');
			$active = $this->getParamInt('active');
			$dataimage = $this->getParamString('img_incens_hd_');//die($dataimage);
            if(!in_array($dataimage, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'article-'.$timenow.'.jpg';
                $path = 'images/article/';
                @mkdir(PROJECT_PATH.'/data/'.$path, 0301);
                $paths = PROJECT_PATH.'/data/'.$path;
                $arr_exp = array('[removed]', '[removed');
                $exp = str_replace($arr_exp,'',$dataimage);
                $this->savefile($exp,$paths,$nameimagess);
                $img = $path.$nameimagess;
            }else{
                $img = '';
            }
			$run = $this->Mstatics->addCampaignUser();
			if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Thêm thành công");window.location.href="'.$this->data['baseurl'].'campaign-user.html";</script>');
            }
        }
        $this->data['action'] = 'addcampaignuser';
        $this->load->view('temp/layout', $this->data);
	}
	
}